package com.wh.wisdomsite.xbox.client.service.configClient.ba;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wh.wisdomsite.xbox.ba.entity.CommonDict;
import com.wh.wisdomsite.xbox.ba.service.ICommonDictService;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.pojo.configClient.ba.CclDictIn;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置端获取字典 Service
 *
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.client.service.configClient.ba
 * @date 2018年11月06日 15:41
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@ServiceProvider()
public class ConfigClientDictService {
    private static final Log logger = LogFactory.getLog(ConfigClientDictService.class);

    @Autowired
    private ICommonDictService iCommonDiceService;

    @ServiceMethod(method = "zhgd.configClient.ba.getDictOption", title = "获取字典列表", version = "1.0", needInSession = NeedInSessionType.YES)
    public CommonResult getDictOption(CclDictIn in) {
        CommonResult commonResult = new CommonResult();
        List<Map<String,String>> dictList = new ArrayList<>();
        try{
            //设置条件
            logger.info("获取项目列表->>zhgd.configClient.ba.getDictOption->>开始");
            EntityWrapper<CommonDict> ewCommonDict = new EntityWrapper<>();
            ewCommonDict.setEntity(new CommonDict());
            if (StringUtils.isNotEmpty(in.getModelCode())) {
                ewCommonDict.eq("modelCode", in.getModelCode());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.ZHGH_CONFIG_COMONDICT_MODELCODE_NOT_FIND);
                return commonResult;
            }
            if (StringUtils.isNotEmpty(in.getFunCode())) {
                ewCommonDict.eq("funCode", in.getFunCode());
            }
            if (StringUtils.isNotEmpty(in.getPropertyParentCode())) {
                ewCommonDict.eq("propertyParentCode", in.getPropertyParentCode());
            }else{
                ewCommonDict.eq("propertyParentCode", "0");
            }
            ewCommonDict.orderBy("propertySort", true);
            //根据条件查询出所有的公共字典，为下拉选框提供数据
            List<CommonDict> commonDictList = iCommonDiceService.selectList(ewCommonDict);
            if (commonDictList.size() > 0) {
                for (CommonDict commonDict : commonDictList) {
                    Map<String,String> dictMap = new HashMap<>();
                    String id = commonDict.getCdId();
                    String propertyName = commonDict.getPropertyName();
                    dictMap.put("value",id);
                    dictMap.put("text",propertyName);
                    dictList.add(dictMap);
                }
            }
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
            commonResult.setResult(dictList);
            logger.info("获取项目列表->>zhgd.configClient.ba.getDictOption->>结束");
        }catch (Exception e){
            logger.error("获取项目列表->>zhgd.configClient.ba.getDictOption->>异常："+e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }
}
